Soru & Cevap

android studio parseInt/getInt/Valueof hatası alıyorum

10.12.2019 - 09:58

Herkese merhaba ben yeni android öğreniyorum ama yaptığım bir uygulamada nesneden aldığım veriyi integer e çeviremiyorum

önce stringe çevirdim sonra integere ama olmadı direkt çevirdim gene olmadı

android studio da kod hata vermiyor ama telefonda da açılmıyor her zaman uygulama durduruldu hatası alıyorum

bu java nın kodu(daha tamamlanmadı):

package com.yf.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
	Random r1=new Random();
	Random r2=new Random();
	Random operation_r=new Random();
	TextView resault,number_one,operation,number_two;
	EditText resault_input;
	Button change;
	int s1,s2,sonuc,operation_i,input;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		number_one=(TextView)findViewById(R.id.number_one);
		operation=(TextView)findViewById(R.id.operation);
		number_two=(TextView)findViewById(R.id.number_two);
		resault=(TextView)findViewById(R.id.resault);
		resault_input=(EditText) findViewById(R.id.resault_input);
		change=(Button)findViewById(R.id.change);
		tick();
		p_random();
	}
	@SuppressLint("ResourceAsColor")
	public void p_random(){
		s1=r1.nextInt(999)+1;
		s2=r2.nextInt(999)+1;
		operation_i=operation_r.nextInt(4)+1;
		number_one.setText(""+s1);
		number_two.setText(""+s2);
		input=Integer.parseInt(resault_input.getText().toString());
		if ( operation_i==1 ) {
			sonuc=s1+s2;
			operation.setText(" + ");
			if ( input==sonuc ){resault.setTextColor(R.color.green);resault.setText("TRUE");
			}
			else {resault.setText("FALSE\n"+sonuc); resault.setTextColor(R.color.red);}
		}
		else if ( operation_i==2 ) {
			sonuc=s1-s2;
			operation.setText(" - ");
			resault.setText(""+sonuc);
		}
		else if ( operation_i==2 ) {
			sonuc=s1*s2;
			operation.setText(" x ");
			resault.setText(""+sonuc);
		}
		else if ( operation_i==4 ){
			sonuc=s1/s2;
			operation.setText(" / ");
			resault.setText(""+sonuc);
		}
	}
	public void tick(){
		change.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				p_random();
			}
		});
	}
}

bu da xml kodu:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:app="http://schemas.android.com/apk/res-auto"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	tools:context=".MainActivity"
	android:gravity="center"
	android:orientation="vertical">
<LinearLayout
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:orientation="horizontal">
	<TextView
		android:id="@+id/number_one"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/>
	<TextView
		android:id="@+id/operation"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/>
	<TextView
		android:id="@+id/number_two"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/>
</LinearLayout>
	<EditText
		android:id="@+id/resault_input"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:ems="8"
		android:textSize="20dp"
		android:inputType="number"/>
	<Button
		android:id="@+id/change"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="change"
		android:background="@color/colorbackgraund"/>
	<TextView
		android:id="@+id/resault"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/>
</LinearLayout>

yardımlarınız için şimdiden teşekkürler

 

76 Görüntülenme

2 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

Profile picture for user kahtali2704
kahtali2704
13.12.2019 - 09:57

sonunda yaptım

işte:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
	Random r1=new Random();
	Random r2=new Random();
	Random operation_r=new Random();
	TextView resault,number_one,operation,number_two;
	EditText resault_input;
	Button change;
	String input;
	int s1,s2,sonuc,operation_i;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		number_one=(TextView)findViewById(R.id.number_one);
		operation=(TextView)findViewById(R.id.operation);
		number_two=(TextView)findViewById(R.id.number_two);
		resault=(TextView)findViewById(R.id.resault);
		resault_input=(EditText) findViewById(R.id.resault_input);
		change=(Button)findViewById(R.id.change);
		p_random();
		tick();
	}
	public void p_random(){
		s1=r1.nextInt(999)+1;
		s2=r2.nextInt(999)+1;
		operation_i=operation_r.nextInt(4)+1;
		number_one.setText(""+s1);
		number_two.setText(""+s2);
		if ( operation_i==1 ) {
			sonuc=s1+s2;
			operation.setText(" + ");}
		else if ( operation_i==2 ) {
			sonuc=s1-s2;
			operation.setText(" - ");}
		else if ( operation_i==3 ) {
			sonuc=s1*s2;
			operation.setText(" x ");}
		else if ( operation_i==4 ){
			sonuc=s1/s2;
			operation.setText(" / ");}
	}
	public void tick(){
		change.setOnClickListener(new View.OnClickListener() {
			@SuppressLint("ResourceAsColor")
			@Override
			public void onClick(View v) {
				input=String.valueOf(sonuc);
				if (input.equals(resault_input.getText().toString())){
					resault.setText("TRUE");
					resault.setTextColor(R.color.green);
				}
				else {
					resault.setText("FALSE\n"+sonuc);
					resault.setTextColor(R.color.red);
				}
				p_random();
			}
		});
	}
}

 

Profile picture for user kahtali2704
kahtali2704
12.12.2019 - 08:50

Yazmayı unuttuğumm bir şey var edittexten veri alabliyorum, stringe atayabilitroum ama stringden integer e çeviremiyorum